package com.nanyang.tms.api.dto.request.product.add;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiRequest;
import com.nanyang.common.core.exception.ValidateException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Data
public class ProductBaseAddRequest  extends ApiRequest {

    @JsonIgnore
    private String pin;
    @Valid
    @NotNull(message = "productAddRequest不能为空")
    private ProductAddRequest productAddRequest;
    @Valid
    @NotNull(message = "pruductRuleAddRequest不能为空")
    private PruductRuleAddRequest pruductRuleAddRequest;
    @Valid
    @NotNull(message = "productCarryRuleAddRequestList不能为空")
    private List<ProductCarryRuleAddRequest> productCarryRuleAddRequestList;
    @Valid
    @NotNull(message = "productWeightModelAddRequest不能为空")
    private ProductWeightModelAddRequest productWeightModelAddRequest;
    @Valid
    @NotNull(message = "productPartAddRequestList不能为空")
    private List<ProductPartAddRequest> productPartAddRequestList;
    @Override
    public void requestCheck() {

        //1、产品计重规则表 国家、城市、货物类型唯一；国家、城市为空时候设置未ALL，有城市信息 国家信息不能为空
            List<String> checkWeightModelList=new ArrayList<>();
            List<ProductWeightRuleAddRequest> productWeightRuleAddRequestList = productWeightModelAddRequest.getProductWeightRuleAddRequestList();
            for (ProductWeightRuleAddRequest productWeightRuleAddRequest : productWeightRuleAddRequestList) {
                if(StringUtils.isNotEmpty(productWeightRuleAddRequest.getCityCode())&&StringUtils.isEmpty(productWeightRuleAddRequest.getCountyCode())){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计重规则 国家信息不能为空！");
                }
                if(StringUtils.isEmpty(productWeightRuleAddRequest.getCityCode())){
                    productWeightRuleAddRequest.setCityCode("ALL");
                    productWeightRuleAddRequest.setCityName("ALL");
                }
                if(StringUtils.isEmpty(productWeightRuleAddRequest.getCountyCode())){
                    productWeightRuleAddRequest.setCountyCode("ALL");
                    productWeightRuleAddRequest.setCountyName("ALL");
                }
                String weightModelStr=productWeightRuleAddRequest.getCountyCode()+"-"+productWeightRuleAddRequest.getCityCode()+"-"+productWeightRuleAddRequest.getCargoTypeCode();
                if(checkWeightModelList.contains(weightModelStr)){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计重规则 国家、城市、货物类型信息唯一，不能存在重复");
                }
                checkWeightModelList.add(weightModelStr);
            }
        //2、计费重进位规则 起始重量和截止重量 区间唯一
        List<String> checkCarryRuleList=new ArrayList<>();
        for (ProductCarryRuleAddRequest productCarryRuleAddRequest : productCarryRuleAddRequestList) {
            String carryRuleStr=productCarryRuleAddRequest.getWeightStart()+"-"+productCarryRuleAddRequest.getWeightEnd();
            if(checkCarryRuleList.contains(carryRuleStr)){
                throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计费重进位规则 起始重量和截止重量区间唯一，不能存在重复");
            }
            checkCarryRuleList.add(carryRuleStr);
        }
    }

}
